package com.example.demo.service.impl;

import com.example.demo.mapper.DeptMapper;
import com.example.demo.mapper.EmpMapper;
import com.example.demo.pojo.Dept;
import com.example.demo.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;
@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EmpMapper empMapper;

    @Override
    public List<Dept> list() {
        return deptMapper.list();
    }
    @Transactional(rollbackFor = Exception.class)//当前方法交给spring进行事务管理（成功：提交 失败：回滚） (默认：RuntimeException运行时异常才能回滚)     (拦截所有异常)
    @Override
    public void delete(Integer id) {
        deptMapper.delete(id);
        int i = 1/0;
        empMapper.deleteById(id);
    }

    @Override
    public void add(Dept dept) {
        dept.setCreateTime(LocalDate.now());
        dept.setUpdateTime(LocalDate.now());
        deptMapper.insert(dept);
    }

    @Override
    public void modify(Dept dept) {
        dept.setUpdateTime(LocalDate.now());
        deptMapper.modify(dept);
    }
}
